package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取地址列表
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,userid);
        lambdaQueryWrapper.eq(AddressBook::getIsDeleted,0);
        lambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R add (@RequestBody AddressBook addressBook){
        //LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Long user_Id = BaseContext.getCurrentId();
        addressBook.setUserId(user_Id);
        addressBookService.save(addressBook);
        return R.success(null);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
     public R defaultAddress(@RequestBody AddressBook addressBook){
    Long id = addressBook.getId();
    Long user_id = BaseContext.getCurrentId();
    //现将不等于传入id的地址都设为0;
    LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
    lambdaUpdateWrapper.ne(AddressBook::getId,id);
    lambdaUpdateWrapper.eq(AddressBook::getUserId,user_id);
    lambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
    addressBookService.update(lambdaUpdateWrapper);
    //将传入的id的地址默认地址改为1
    LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper2 = new LambdaUpdateWrapper<>();
    lambdaUpdateWrapper2.eq(AddressBook::getId,id);
    lambdaUpdateWrapper2.set(AddressBook::getIsDefault,1);
    addressBookService.update(lambdaUpdateWrapper2);
    return R.success(null);
}
   @GetMapping("/default")
   public R<AddressBook> getdefault(){
       Long user_id = BaseContext.getCurrentId();
       LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
       addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,user_id);
       addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
       AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
       return R.success(addressBook);
   }




    /**
     * 回显修改数据
     * @param id
     * @return
     */
       @GetMapping("/{id}")
      public R<AddressBook> getById(@PathVariable Long id){
           Long user_id = BaseContext.getCurrentId();
           LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
           addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,user_id);
           addressBookLambdaQueryWrapper.eq(AddressBook::getId,id);
           AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
           return R.success(addressBook);
       }

    /**
     * 修改
     * @param addressBook
     * @return
     */
    @PutMapping
       public R update(@RequestBody AddressBook addressBook){
           addressBookService.updateById(addressBook);
           return R.success(null);
       }

    /**
     * 删除
     * @param ids
     * @return
     */
       @DeleteMapping
       public R remove(Long ids){
           Long user_id = BaseContext.getCurrentId();
           LambdaUpdateWrapper<AddressBook> lambdaQueryWrapper = new LambdaUpdateWrapper<>();
           lambdaQueryWrapper.eq(AddressBook::getId,ids);
           lambdaQueryWrapper.eq(AddressBook::getUserId,user_id);
           lambdaQueryWrapper.set(AddressBook::getIsDefault,0);
           lambdaQueryWrapper.set(AddressBook::getIsDeleted,1);
           addressBookService.update(lambdaQueryWrapper);
           return R.success(null);
       }

}
